package com.inke.box.setting.Adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;


import java.util.ArrayList;

/**
 * Created by Administrator on 2017/11/28.
 */
public class SettingMainAdapter extends FragmentStatePagerAdapter {
    Context mContext;
    String[] titles=null;
    private ArrayList<Class<?extends Fragment>> fragments = new ArrayList<>();
    /**
     *
     * @param fm  FragmentManager 管理器
     * @param context  上下文
     */
    public SettingMainAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.mContext=context;

    }
    public int setTabTitle(int arrayid){
        titles=mContext.getResources().getStringArray(arrayid);
        return arrayid;
    }
    //对外提供添加Fragment的方法
    public void addPage(Class<?extends Fragment> clz){
        fragments.add(clz);
    }
    @Override
    public Fragment getItem(int position) {
        Fragment fragment=Fragment.instantiate(mContext,fragments.get(position).getName());
//        try {
//            fragment= fragments.get(position).newInstance();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
        return fragment;
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}


